#pragma once

#include "const.h"

struct SectionInfo
{
	SectionInfo() = default;
	~SectionInfo() {
		m_section_datas.clear();
	}

	SectionInfo(const SectionInfo& src) {
		m_section_datas = src.m_section_datas;
	}

	SectionInfo& operator=(const SectionInfo& src) {
		if (&src == this) {
			return *this;
		}

		m_section_datas = src.m_section_datas;
		return *this;
	}

	std::string operator[](const std::string& key) {
		if (m_section_datas.find(key) == m_section_datas.end()) {
			return "";
		}
		
		return m_section_datas[key];
	}

	std::string getValue(const std::string& key) {
		if (m_section_datas.find(key) == m_section_datas.end()) {
			return "";
		}

		return m_section_datas[key];
	}

	std::map<std::string, std::string> m_section_datas;
};

class ConfigManager
{
public:
	~ConfigManager() {
		m_config_map.clear();
	}

	ConfigManager(const ConfigManager& src) = delete;
	ConfigManager& operator=(const ConfigManager& src) = delete;

	SectionInfo operator[](const std::string& section) {
		if (m_config_map.find(section) == m_config_map.end()) {
			return SectionInfo();
		}
		return m_config_map[section];
	}

	static ConfigManager& getInstance() {
		static ConfigManager cfg_mgr;
		return cfg_mgr;
	}

	std::string getValue(const std::string& section, const std::string& key);

private:
	ConfigManager();
	
private:
	// 存储sectionInfo和key-value对的map  
	std::map<std::string, SectionInfo> m_config_map;
};

